﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;

namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAEK02LIST : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public YCAEK02LISTData m_Data, m_DataX;
        public DataSet dsParamaters;
        public YCAEK02LISTReport m_rpt;
        bool IsSupplierLoded = false;
        public string PrnType = "EXCEL";
        BindingListCollectionView cvEK0201
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgEK0201.ItemsSource);
            }
        }
        BindingListCollectionView cvEK0202
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgEK0202.ItemsSource);
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        public SETINTEXDB Set4ITX2
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX2");
            }
        }
        public SETINTEXDB Set4ITX
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX");
            }
        }
        
        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion
        
        #region=====vwEKKOPF=======
        CollectionViewSource vwEKKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKKOPF");
            }
        }
        DataRow dwEKKOPF
        {
            get
            {
                if (vwEKKOPF.View == null)
                    return null;
                if ((DataRowView)vwEKKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKKOPF.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS=======
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }
        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS2=======
        CollectionViewSource vwEKPOS2
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS2");
            }
        }
        DataRow dwEKPOS2
        {
            get
            {
                if (vwEKPOS2.View == null)
                    return null;
                if ((DataRowView)vwEKPOS2.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS2.View.CurrentItem).Row;
            }
        }
        #endregion
        
        #endregion
        string Year
        {
            get
            {
                if (txtYear.Text.Trim() == "")
                {
                    MessageBox.Show("请设定年限");
                }
                return Helpers.Helper.Left(txtYear.Text.ToString()+"    ",5);
            }
            set
            {
                txtYear.Text = value;
            }
        }
        public YCAEK02LIST()
        {
            InitializeComponent();
            this.Name = "采购统计";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new YCAEK02LISTData(OracleDB,proxy);
            if (!IsSupplierLoded)
            {
                SetEnv.Merge(m_Data.LoadSupplier());
                IsSupplierLoded = true;
                DataRow dwData = SetData.FSSTAMM.NewRow();
                dwData["FSMANDANT"] = "00";
                DataSet SResult = m_Data.INTEXDBC.QueryFSSTAMM(dwData, 7);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    txtYear.Text = row["FSKOLL"].ToString();
                    txtSYear.Text = txtYear.Text;
                }
            }  
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
             
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        public void btnQuery1_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            try
            {
                DataRow dwData = SetData.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = "00";
                dwData["FSKOLL"] = Year;
                dwData["FSABEZ"] = Helpers.Helper.ParseSectionStr(txtSection.Text);
                dwData["ABPART"] = txtArt.Text.Trim();
                dwData["ABAART"] = txtFArt.Text.Trim();       
                Result = m_Data.BuildData(dwData,1);
                Set4ITX2.Clear();
                Set4ITX2.Merge(Result);
                if (vwEKPOS2.View != null)
                {
                    vwEKPOS2.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            try
            {
                if (txtSection.Text.Trim() == "")
                {
                    MessageBox.Show("请指定季节");
                    return;
                }
                DataRow dwData = SetData.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = "00";
                dwData["FSKOLL"] = Year;
                dwData["ABPART"] = Helpers.Helper.ParseSectionStr(txtSection.Text);
                Result = m_Data.BuildData(dwData);
                Set4ITX.Clear();
                Set4ITX.Merge(Result);
                if (vwEKPOS.View != null)
                {
                    vwEKPOS.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnFilterEK0201_Click(object sender, RoutedEventArgs e)
        {
            string filter = "LSLIEFNA ='" + dwLIEFDAT["LSLIEFNA"].ToString() + "'";
            cvEK0201.CustomFilter = filter;
        }
        private void btnFilterEK0201Clear_Click(object sender, RoutedEventArgs e)
        {
            cvEK0201.CustomFilter = null;
        }
        private void btnFilterEK0202_Click(object sender, RoutedEventArgs e)
        {
            string filter = "ABPART LIKE '%" + txtArt.Text.ToString().Trim() + "%' AND ABAART LIKE '%" + txtFArt.Text.ToString().Trim() + "%'";
            cvEK0202.CustomFilter = filter;
        }
        private void btnFilterEK0202Clear_Click(object sender, RoutedEventArgs e)
        {
            cvEK0202.CustomFilter = null;
        } 
            public void btnPrintPreview1_Click(object sender, EventArgs e)
        {
            PrnType = "OpenExcel";
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                DataSet setRpt = new DataSet();

                if (setRpt == null)
                    return;
                if (m_DataX == null)
                {
                    m_DataX = new YCAEK02LISTData(dicParamater);
                }
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                if (m_DataX.exAgent._app == null)
                {
                    m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                YCAEKAB15Report m_rpt1 = new YCAEKAB15Report(dicParamater);
                m_rpt1.dsSource = Set4ITX2;
                m_rpt1.exAgent = m_DataX.exAgent;
                m_rpt1.OracleDB = this.OracleDB;
                m_rpt1.proxy = this.proxy;
                m_rpt1.WriteExcelReport(PrnType);
            }
        }
        private void cbSECTION_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ComboBox cb = sender as ComboBox;
                txtSection.Text += cb.SelectedValue + ";";
            }
        public void btnPrintPreview_Click(object sender, EventArgs e)
        {
            PrnType = "OpenExcel";
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                DataSet setRpt = new DataSet();

                if (setRpt == null)
                    return;
                if (m_DataX == null)
                {
                    m_DataX = new YCAEK02LISTData(dicParamater);
                }
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                if (m_DataX.exAgent._app == null)
                {
                    m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt = new YCAEK02LISTReport(dicParamater);
                m_rpt.dsSource = Set4ITX;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy;
                m_rpt.WriteExcelReport(PrnType);
            }
        } 
    }
}
